﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;

namespace Catering.Catering_Page.Caipin
{
    public partial class shiputzypj_Manage : UserIsNull
    {
        View_Operaction_Common voc = new View_Operaction_Common();
        public DataTable dt_DishType = new DataTable();
        public DataTable dt_DishTypes = new DataTable();
        public DataTable dt_Dish = new DataTable();
        public DataTable dt_library = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["DishList"] = voc.UITreeViewDataBind_DishTypes("1");
                ViewState["shipulist"] = DataBingShipuList("1", "1");

                this.LoadData();
            }
        }

        //加载页面数据
        public void LoadData()
        {
            this.dt_DishType = new Catering_Rules.DishType().GetAllList().Tables[0];
            this.dt_DishTypes = new Catering_Rules.DishTypes().GetAllList().Tables[0];
        }

        //加载菜
        public void LoadDish(string strTypes)
        {
            this.dt_Dish = new Catering_Rules.T_Dish().GetList("DTID=" + strTypes).Tables[0];
        }

        //加载菜谱库
        public void LoadLibrary()
        {
            if (Request.QueryString["cid"] == null)//食谱ID
                return;

            string cid = Request.QueryString["cid"].ToString();
            string tedianID = "";
            Catering_Model.E_Recipes md_R = new Catering_Rules.T_Recipes().GetModel(Convert.ToInt32(cid));
            if (Request.QueryString["IsGroupCatering"] != null)//群体配餐
            {
                Catering_Model.GroupCatering md_GC = new Catering_Rules.GroupCatering().GetModel(md_R.GroupID);
                tedianID = md_GC.temp2;
            }
            else
            {
                Catering_Model.PersonalService md_PS = new Catering_Rules.PersonalService().GetModel(md_R.PID);
                tedianID = md_PS.temp2;
            }

            this.dt_library = new Catering_Rules.library().GetList("temp1='" + tedianID + "'").Tables[0];
        }

        protected void Dr_zhoulist_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void but_Save_Click(object sender, EventArgs e)
        {

        }

        public string DataBingShipuList(string strClassID, string strGroupID)
        {
            Catering_Rules.T_Recipes crr = new Catering_Rules.T_Recipes();
            List<Catering_Model.E_Recipes> list = crr.GetModelList("AreaID=" + strGroupID + "  and ClassIDs=" + strClassID);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (Catering_Model.E_Recipes cmr in list)
            {
                sb.Append("<tr>");
                sb.Append("<td> <input type=\"CheckBox\" id=\"ck" + cmr.id + "\" /></td>");

                sb.Append("<td>" + cmr.CateringName + "</td>");
                sb.Append("<td>" + cmr.StateDate + "</td>");
                sb.Append("<td>" + cmr.StopDate + "</td>");
                sb.Append("<td>" + cmr.Number + "</td>");
                sb.Append("<td>" + cmr.Back + "</td>");
                sb.Append("</tr>");
            }
            return sb.ToString();

        }

        //加载对应食谱库
        [WebMethod]
        public static ArrayList Getlibrary(string RecipesID)
        {
            ArrayList al = new ArrayList();
            StringBuilder sb = new StringBuilder();
            DataTable dt_library = new DataTable();
            Catering_Model.E_Recipes md_Recipes = new Catering_Rules.T_Recipes().GetModel(Convert.ToInt32(RecipesID));
            if (md_Recipes.PID == 0)//群体配餐
            {
                int GroupID = md_Recipes.GroupID;//群体配餐ID
                DataTable dt_CateringGroupInfo = new Catering_Rules.CateringGroupInfo().GetList(1, "GroupID=" + GroupID, "id").Tables[0];
                int TedianID = Convert.ToInt32(dt_CateringGroupInfo.Rows[0]["Group_tedianID"].ToString());
                dt_library = new Catering_Rules.library().GetList("temp1='" + TedianID.ToString() + "'").Tables[0];
            }
            else
            {
                int PID = md_Recipes.PID;
                Catering_Model.PersonalService md_PS = new Catering_Rules.PersonalService().GetModel(PID);
                dt_library = new Catering_Rules.library().GetList("temp1='" + md_PS.temp2 + "'").Tables[0];
            }

            if (dt_library != null && dt_library.Rows.Count > 0)
            {
                al.Add("OK");
                int i = 1;
                string tempid = "";
                string templname = "";

                DataRow[] t_r = dt_library.Select("LName='北方菜谱'");
                if (t_r != null && t_r.Count() > 0)
                { 
                
                }
                foreach (DataRow row in dt_library.Rows)
                {
                    sb.Append("<tr uid=\"" + row["id"].ToString() + "\" uname=\"" + row["LName"].ToString() + "\"><td>" + i + "</td>");
                    sb.Append("<td>" + row["LName"].ToString() + "</td>");
                    sb.Append("<td>" + row["Back"].ToString() + "</td></tr>");
                    if (row["LName"].ToString() == "北方菜谱")
                    {
                        tempid =row["id"].ToString();
                        templname = row["LName"].ToString();
                    }
                    i += 1;
                }
                al.Add(sb.ToString());
                if (tempid == "" || templname == "")
                {
                    al.Add(dt_library.Rows[0]["id"].ToString());
                    al.Add(dt_library.Rows[0]["LName"].ToString());
                }
                else
                {
                    al.Add(tempid);
                    al.Add(templname);
                }
            }
            else
            {
                al.Add("NO");
            }
            return al;
        }


        //加载菜谱详细
        [WebMethod]
        public static ArrayList GetDishInfoByLish(string Lid)
        {
            ArrayList al = new ArrayList();
            StringBuilder sb = new StringBuilder();
            DataTable dt_DishType = new Catering_Rules.DishType().GetAllList().Tables[0];
            DataTable dt_DishTypes = new Catering_Rules.DishTypes().GetAllList().Tables[0];
            int dishnum = 0;
            //int classid = 0;
            //Catering_Model.Recipes re=new Catering_Rules.Recipes().GetModel(Convert.ToInt32(RecipesID));
            //if (re != null) classid = re.ClassID;
            if (dt_DishType != null && dt_DishType.Rows.Count > 0)
            {
                foreach (DataRow row_type in dt_DishType.Rows)
                {
                    sb.Append("<li><a IsCai=\"false\">" + row_type["DT"].ToString() + "</a><ul class=\"treeview\">");
                    if (dt_DishTypes != null && dt_DishTypes.Rows.Count > 0)
                    {
                        if (dt_DishTypes.Select("DTID=" + row_type["id"].ToString()).Count() == 1 && dt_DishTypes.Select("DTID=" + row_type["id"].ToString())[0]["DTName"].ToString() == row_type["DT"].ToString())
                        {
                            DataTable dt_Dish = new Catering_Rules.library_Dish().GetList("libraryID=" + Lid + " and DishTypeID=" + dt_DishTypes.Select("DTID=" + row_type["id"].ToString())[0]["id"].ToString()).Tables[0];
                            //DataTable dt_Dish = new Catering_Rules.Dish().GetList("DTID=" + dt_DishTypes.Select("DTID=" + row_type["id"].ToString())[0]["id"].ToString()).Tables[0];
                            if (dt_Dish != null && dt_Dish.Rows.Count > 0)
                            {
                                foreach (DataRow row_dish in dt_Dish.Rows)
                                {
                                    Catering_Model.E_Dish md = new Catering_Rules.T_Dish().GetModel(Convert.ToInt32(row_dish["DishID"].ToString()));
                                    if (md != null)
                                    {
                                        sb.Append("<li><a IsCai=\"true\" uid=\"" + md.id + "\">" + md.Name + "</a></li>");
                                        dishnum++;
                                    }
                                }
                            }
                        }
                        else
                        {
                            foreach (DataRow row_typeinfo in dt_DishTypes.Select("DTID=" + row_type["id"].ToString()))
                            {
                                sb.Append("<li><a  IsCai=\"false\">" + row_typeinfo["DTName"].ToString() + "</a><ul>");
                                DataTable dt_Dish = new Catering_Rules.library_Dish().GetList("libraryID=" + Lid + " and DishTypeID=" + row_typeinfo["id"].ToString()).Tables[0];
                                //DataTable dt_Dish = new Catering_Rules.Dish().GetList("DTID=" + row_typeinfo["id"].ToString()).Tables[0];
                                if (dt_Dish != null && dt_Dish.Rows.Count > 0)
                                {
                                    foreach (System.Data.DataRow row_dish in dt_Dish.Rows)
                                    {
                                        Catering_Model.E_Dish md = new Catering_Rules.T_Dish().GetModel(Convert.ToInt32(row_dish["DishID"].ToString()));
                                        if (md != null)
                                        {
                                            sb.Append("<li><a IsCai=\"true\" uid=\"" + md.id + "\">" + md.Name + "</a></li>");
                                            dishnum++;
                                        }
                                    }
                                }
                                sb.Append("</ul></li>");
                            }
                        }
                    }
                    sb.Append("</ul></li>");
                }
            }
            al.Add(sb.ToString());
            al.Add(dishnum);
            return al;
        }
    }
}